package com.tanguy.driver.ui

import cn.sinata.xldutils_kotlin.utils.SPUtils
import com.tanguy.driver.Const
import com.tanguy.driver.R
import com.tanguy.driver.base.ImmersionTitleActivity
import com.tanguy.driver.net.http.HttpManager
import com.tanguy.driver.ui.account.activity.LoginActivity
import com.tanguy.driver.ui.driver.DriverMainActivity
import com.tanguy.driver.ui.operator.OperatorMainActivity
import com.tanguy.driver.utlis.extend.request
import org.jetbrains.anko.startActivity

/**
 * Created by dab on 2017/11/20 0020 11:37
 * 启动页
 */
class LauncherActivity : ImmersionTitleActivity() {
    override fun setContentViewRes(): Int = R.layout.activity_launcher
    override fun fullScreen(): Boolean = true
    override fun initView() {
        super.initView()
        val phone = SPUtils.instance().getString(Const.User.PHONE)
        val password = SPUtils.instance().getString(Const.User.PASSWORD)
        if (phone.isEmpty() || password.isEmpty()) {
            startActivity<LoginActivity>()
            finish()
        } else {
            HttpManager.login(phone, password)
                    .request(this, false) {
                        if (it != null && it.code == 0 && it.data != null) {
                            val id = it.data!!.get("id").asInt
                            val type = it.data!!.get("type").asInt
                            if (type == 1) {
                                //司机
                                startActivity<DriverMainActivity>(Const.User.ID to id)
                                finish()
                            } else {
                                //运营商
                                startActivity<OperatorMainActivity>(Const.User.ID to id)
                                finish()
                            }
                        } else {
                            startActivity<LoginActivity>()
                            finish()
                        }

                    }

        }

    }
}